package com.stylefeng.guns.modular.cms.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.util.ToolUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.CmsArticleType;
import com.stylefeng.guns.modular.cms.service.ICmsArticleTypeService;


/**
 * 文章类型管理控制器
 *
 * @author fengshuonan
 * @Date 2018-06-25 00:00:01
 */
@Controller
@RequestMapping("/cmsArticleType")
public class CmsArticleTypeController extends BaseController {

    private String PREFIX = "/cms/cmsArticleType/";

    @Autowired
    private ICmsArticleTypeService cmsArticleTypeService;

    /**
     * 跳转到文章管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "cmsArticleType.html";
    }

    /**
     * 跳转到添加文章管理
     */
    @RequestMapping("/cmsArticleType_add")
    public String cmsArticleTypeAdd() {
        return PREFIX + "cmsArticleType_add.html";
    }

    /**
     * 跳转到修改文章管理
     */
    @RequestMapping("/cmsArticleType_update/{cmsArticleTypeId}")
    public String cmsArticleTypeUpdate(@PathVariable Integer cmsArticleTypeId, Model model) {
        CmsArticleType cmsArticleType = cmsArticleTypeService.selectById(cmsArticleTypeId);
        model.addAttribute("item",cmsArticleType);
        LogObjectHolder.me().set(cmsArticleType);
        return PREFIX + "cmsArticleType_edit.html";
    }

    /**
     * 获取文章管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        if(ToolUtil.isEmpty(condition)){
            return cmsArticleTypeService.selectList(null);
        }else{
            Wrapper<CmsArticleType> wrapper = new EntityWrapper<CmsArticleType>();

            return cmsArticleTypeService.selectList(wrapper.like("title", condition));
        }
    }

    /**
     * 新增文章管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(CmsArticleType cmsArticleType) {
        cmsArticleTypeService.insert(cmsArticleType);
        return SUCCESS_TIP;
    }

    /**
     * 删除文章管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer cmsArticleTypeId) {
        cmsArticleTypeService.deleteById(cmsArticleTypeId);
        return SUCCESS_TIP;
    }

    /**
     * 修改文章管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(CmsArticleType cmsArticleType) {
        cmsArticleTypeService.updateById(cmsArticleType);
        return SUCCESS_TIP;
    }

    /**
     * 文章管理详情
     */
    @RequestMapping(value = "/detail/{cmsArticleTypeId}")
    @ResponseBody
    public Object detail(@PathVariable("cmsArticleTypeId") Integer cmsArticleTypeId) {
        return cmsArticleTypeService.selectById(cmsArticleTypeId);
    }
}
